package com.html2rss.core;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import javax.xml.xpath.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.ArrayList;

import com.html2rss.models.Query;
import com.html2rss.core.DomHelper;
import com.html2rss.core.exceptions.HtmlToRssException;

/**
 * User: VSlaykovskiy
 * Date: 13.04.2007
 * Time: 14:16:01
 */
public class TestXPath {


    public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, IOException, SAXException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, HtmlToRssException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse("books.xml");
        Node node1 = doc.getChildNodes().item(0).
                         getChildNodes().item(1).
                         getChildNodes().item(1).
                         getChildNodes().item(1);
        Node node2 = doc.getChildNodes().item(0).
                         getChildNodes().item(1).
                         getChildNodes().item(1).
                         getChildNodes().item(5).
                         getChildNodes().item(1);
        ArrayList<Node> lst = new ArrayList<Node>();
        lst.add(node1);
        lst.add(node2);
        Query query = new Query(lst);
        List<Node> nodes;
        nodes = query.execute(doc);
        print(nodes);
        System.out.println("***");
        query.getPathToRootInd().set(1, -1);
        List<Node> nodes2 = query.execute(doc);
        print(nodes);
        if (!nodes.equals(nodes2)) {
            System.out.println("Not equals!!!");
        }

        print(DomHelper.getXPathNodeList("//*[@year='2000']", doc));

    }

    private static void print(List<Node> nodes) {
        for (Node node : nodes) {
            System.out.println((node.getNodeName()));
        }
    }
}
